\section{\LaTeX{} Environments}

A \LaTeX\ environment is a wrapper around a certain part of your code that defines how the contents of that code should behave. Every time you see \verb|\begin| and \verb|\end|, you are looking at an environment. For example, if we have text inside of a center environment, the output document has that text centered. Or if we have information inside of a tabular environment, then we get a table. Technically, the document itself is an environment, as it has \verb|\begin{document}| at the top and \verb|\end{document}| at the end.

\begin{center}
  \fbox{\includegraphics[width=0.85\linewidth]{img-8-1}}
\end{center}

The \verb|\begin{}| command for an environment loads code that defines the behavior of the environment. When we put \verb|\begin{document}| into our code, we are telling it to process all of the basic document information, such as page layout and font size, based on the specific document class and any other packages we're using. When we use \verb|\begin{tabular}| we are adding in extra commands for how to calculate the size of the rows and columns, and how it should draw the various lines that are inside of the table.

The \verb|\end{}| command tells \LaTeX\ that you're done with the environment. When you end an environment, you end everything inside of it. This means you need to make sure you create a nested structure so that all of your environments are ended in the order in which you created them. In fact, if you fail to do this, \LaTeX\ will give you an error complaining about the mismatch of your \verb|\begin| and \verb|\end| commands.

\begin{center}
  \fbox{\includegraphics[width=0.75\linewidth]{img-8-2}}
\end{center}

Here is a list of some of the environments that we have already worked with. It turns out that the math modes are also environments. The two standard math modes are the \texttt{math} and \texttt{displaymath} environments. The delimiters that we use are just a shortcut commands to replace the \verb|\begin| and \verb|\end| commands.

\begin{center}
  \fbox{\includegraphics[width=0.85\linewidth]{img-8-3}}
\end{center}

We will add a few more standard environments to our collection. The first couple are the \texttt{itemize} and \texttt{enumerate} environments. Both of these are environments that create indented lists. The \texttt{itemize} command creates unnumbered lists whereas the \texttt{enumerate} command creates numbered lists. Each new item in the list must be preceded by \verb|\item|.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-8-4}}
\end{center}

With the \texttt{itemize} environment, you can manually replace the marker at the left by including an optional argument after \verb|\item|. For example, you can replace the bullet with a right arrow. If you put in an empty set of brackets, you'll just get the indentation with no symbol at all. All of these symbols are right-justified, so that it will push extra characters to the left. The same thing works with the \texttt{enumerate} environment. However, this will disrupt the counting feature, which can potentially cause a misalignment of your labels.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-8-5}}
\end{center}

There are ways to customize these lists using the \href{https://ctan.org/pkg/enumitem}{\texttt{enumitem}} package. At each level, you can define exactly how you want the labels to look. You can use capital or lower case Roman numerals, capital or lower case alphabetical labels, or Arabic numerals as labels. Plus you can add any type of adornment that you want, such as parentheses. These four examples will give you a good sense of how to do this. This package will also let you adjust various aspects of the spacing, but you will have to read through the documentation on that.

Another important environment for math students is the theorem environment. A theorem environment is actually a general name for many types of theorem-like environments. If you look at any standard upper division math textbook, you will see that definitions, theorems, lemmas, examples, propositions, and other things all have a very consistent look to them. They are also usually also numbered in a specific way. All of these behaviors are controlled by the theorem environments.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-8-6}}
\end{center}

We will use the \href{https://ctan.org/pkg/amsthm}{\texttt{amsthm}} package because it makes things relatively simple. If we wanted to start having automatically numbered theorems, we would just add \verb|\newtheorem{thm}{Theorem}| into the preamble. The first brackets define the name of the environment and the second brackets show what will be displayed. To create a theorem, we would just put the theorem statement in between a \verb|\begin{thm}| and \verb|\end{thm}|. Then every time we wanted to add another theorem, we would do the same thing again.

If we wanted to have a lemma environment, we would just create one in a similar manner. The difficulty with this is that it leads to awkward numbering. The reason for this is that unless we tell \LaTeX{} otherwise, each new theorem environment will have its own counter. If we wanted them to share a counter, we would have to indicate this when we create the lemma environment. We can have many different types of environments using the same counter.

There are ways to have your theorems numbered by section or subsection if you were creating a larger document. But since we haven't discussed sections and subsections yet in this series, I'm going to pass on explaining those parameters. You can find more information in section 3 of the documentation.

If we wanted to create a theorem but not have it numbered, you would use the \verb|\newtheorem*{}{}| command. This is sometimes done with named theorems. For example, it may be more useful to talk about ``the Pythagorean Theorem'' than it is to talk about a theorem with an arbitrary number on it. If you wanted to have both the number and the name, you can add in an optional statement at the start of the theorem environment.

\begin{center}
  \fbox{\includegraphics[width=0.85\linewidth]{img-8-7}}
\end{center}

The \href{https://ctan.org/pkg/amsthm}{\texttt{amsthm}} package has three theorem styles. The \texttt{plain} theorem style has a bolded name and number, and uses italicized font for the text. The \texttt{definition} theorem style has the bolded name and number, but uses a regular font for the text. The \texttt{remark} theorem style has an italicized name, a normal number, and regular font for the text. The reason they are named this way is that these are the common ways they are applied. You could create your own theorem styles, but you'll have to read through the documentation for that one on your own.

\begin{center}
  \fbox{\includegraphics[width=0.5\linewidth]{img-8-8}}
\end{center}

The \href{https://ctan.org/pkg/amsthm}{\texttt{amsthm}} package also has a proof environment. You don't need to define it, and it's used like all of the other environments. This environment declares that you are making a proof at the start, and puts a square box at the end of the proof flush against the right margin. If you wanted to change that symbol, you would have to redefine the \verb|\qedsymbol| command. And you can define this to be basically anything you want it to be. \LaTeX{} will figure out whether it can fit at the end of the current line or if it will need to exist on its own line.

\begin{center}
  \fbox{\includegraphics[width=0.85\linewidth]{img-8-9}}
\end{center}

You can go one step further and define your own environments. All you need for an environment is the name of the environment, \LaTeX{} code to run at the start of it, and \LaTeX{} code to run at the end of it. Here is an example that mimics an unnumbered theorem environment. In this case, the second and third sets of brackets were each placed on their own line for readability. This environment is called just like all the other environments.

And this can also give you a bit of insight as to what's happening in most environments. We can accomplish the same result by simply typing in the code directly and not using environments at all. The environment is just a fancy shortcut.

I think you can go for quite a long time without needing to create environments, so I consider this a somewhat advanced topic. The purpose of this example was just to give you a flavor of what environments are actually doing. Most of the environments that you will end up using will likely come from preexisting packages. And as long as you read the documentation and keep everything properly nested, you should be able to navigate them pretty well.